一、安装 Lua
下载:
http://www.lua.org/download.html
http://www.lua.org/ftp/lua-5.3.5.tar.gz
a.上传到 $HOME 中,解压缩
[test1280@localhost ~]$ ls -l
total 300
drwxr-xr-x. 4 test1280 test1280 58 Jun 26 2018 lua-5.3.5
-rw-r--r--. 1 test1280 test1280 303543 Jun 10 08:35 lua-5.3.5.tar.gz
b.编译 & 安装
[test1280@localhost ~]$ cd lua-5.3.5/ && make linux && make install INSTALL_TOP=$HOME/lua
PS:
报错时:
gcc -std=gnu99 -O2 -Wall -Wextra -DLUA_COMPAT_5_2 -DLUA_USE_LINUX -c -o lua.o lua.c
lua.c:82:10: fatal error: readline/readline.h: No such file or directory
#include <readline/readline.h>
^~~~~~~~~~~~~~~~~~~~~
执行:
[root@localhost ~]# yum install -y readline-devel
重新执行上述命令,完成编译&安装。
[test1280@localhost ~]$ cd lua-5.3.5/ && make linux && make install INSTALL_TOP=$HOME/lua
完成后,已安装lua到$HOME/lua中。
[test1280@localhost ~]$ ll
total 300
drwxrwxr-x. 7 test1280 test1280 67 Jun 10 10:12 lua
drwxr-xr-x. 4 test1280 test1280 58 Jun 10 10:07 lua-5.3.5
-rw-r--r--. 1 test1280 test1280 303543 Jun 10 08:35 lua-5.3.5.tar.gz
c.更新环境变量,并退出重新登陆主机(使环境变量生效):
[test1280@localhost ~]$ echo 'export PATH=$HOME/lua/bin:$PATH' >> $HOME/.bash_profile
二、安装luarocks
下载:
https://github.com/luarocks/luarocks/wiki/Download
http://luarocks.org/releases/luarocks-3.3.1.tar.gz
a.上传到 $HOME 中,解压缩
[test1280@localhost ~]$ tar zxf luarocks-3.3.1.tar.gz
[test1280@localhost ~]$ ll
total 540
drwxrwxr-x. 7 test1280 test1280 67 Jun 10 10:12 lua
drwxr-xr-x. 4 test1280 test1280 58 Jun 10 10:07 lua-5.3.5
-rw-r--r--. 1 test1280 test1280 303543 Jun 10 08:35 lua-5.3.5.tar.gz
drwxr-xr-x. 5 test1280 test1280 4096 Feb 7 05:24 luarocks-3.3.1
-rw-r--r--. 1 test1280 test1280 241636 Jun 9 02:01 luarocks-3.3.1.tar.gz
b.编译 & 安装
[test1280@localhost ~]$ cd luarocks-3.3.1/ && ./configure --prefix=$HOME/luarocks --with-lua=$HOME/lua && make && make install
完成后,已安装luarocks到$HOME/luarocks中。
[test1280@localhost ~]$ ll
total 540
drwxrwxr-x. 7 test1280 test1280 67 Jun 10 10:12 lua
drwxr-xr-x. 4 test1280 test1280 58 Jun 10 10:07 lua-5.3.5
-rw-r--r--. 1 test1280 test1280 303543 Jun 10 08:35 lua-5.3.5.tar.gz
drwxrwxr-x. 5 test1280 test1280 41 Jun 10 10:22 luarocks
drwxr-xr-x. 8 test1280 test1280 4096 Jun 10 10:22 luarocks-3.3.1
-rw-r--r--. 1 test1280 test1280 241636 Jun 9 02:01 luarocks-3.3.1.tar.gz
c.更新环境变量,并退出重新登陆主机(使环境变量生效):
[test1280@localhost ~]$ echo 'export PATH=$HOME/luarocks/bin:$PATH' >> $HOME/.bash_profile
三、安装libyaml库
lyaml库会依赖于libyaml库,因此,要先安装libyaml库。
下载:
https://pyyaml.org/wiki/LibYAML
http://pyyaml.org/download/libyaml/yaml-0.2.5.tar.gz
a.上传到 $HOME 中,解压缩
[test1280@localhost ~]$ ll
total 628
drwxrwxr-x. 11 test1280 test1280 4096 Jun 1 17:19 libyaml-0.2.5
-rw-r--r--. 1 test1280 test1280 85055 Jun 10 09:11 libyaml-0.2.5.tar.gz
drwxrwxr-x. 7 test1280 test1280 67 Jun 10 10:12 lua
drwxr-xr-x. 4 test1280 test1280 58 Jun 10 10:07 lua-5.3.5
-rw-r--r--. 1 test1280 test1280 303543 Jun 10 08:35 lua-5.3.5.tar.gz
drwxrwxr-x. 5 test1280 test1280 41 Jun 10 10:22 luarocks
drwxr-xr-x. 8 test1280 test1280 4096 Jun 10 10:22 luarocks-3.3.1
-rw-r--r--. 1 test1280 test1280 241636 Jun 9 02:01 luarocks-3.3.1.tar.gz
b.编译 & 安装
[test1280@localhost ~]$ cd libyaml-0.2.5/ && ./bootstrap && ./configure --prefix=$HOME/libyaml && make && make install
PS:
报错1:
./bootstrap: line 3: exec: autoreconf: not found
解决1:
[root@localhost ~]# yum install -y automake
报错2:
configure.ac:56: error: possibly undefined macro: AC_PROG_LIBTOOL
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
autoreconf: /usr/bin/autoconf failed with exit status: 1
解决2:
[root@localhost ~]# yum install -y libtool
重新执行上述命令,完成编译&安装。
[test1280@localhost ~]$ cd libyaml-0.2.5/ && ./bootstrap && ./configure --prefix=$HOME/libyaml && make && make install
完成后,已安装libyaml到$HOME/libyaml中。
[test1280@localhost ~]$ ll
total 628
drwxrwxr-x. 4 test1280 test1280 32 Jun 10 10:31 libyaml
drwxrwxr-x. 13 test1280 test1280 4096 Jun 10 10:30 libyaml-0.2.5
-rw-r--r--. 1 test1280 test1280 85055 Jun 10 09:11 libyaml-0.2.5.tar.gz
drwxrwxr-x. 7 test1280 test1280 67 Jun 10 10:12 lua
drwxr-xr-x. 4 test1280 test1280 58 Jun 10 10:07 lua-5.3.5
-rw-r--r--. 1 test1280 test1280 303543 Jun 10 08:35 lua-5.3.5.tar.gz
drwxrwxr-x. 5 test1280 test1280 41 Jun 10 10:22 luarocks
drwxr-xr-x. 8 test1280 test1280 4096 Jun 10 10:22 luarocks-3.3.1
-rw-r--r--. 1 test1280 test1280 241636 Jun 9 02:01 luarocks-3.3.1.tar.gz
c.更新环境变量,并退出重新登陆主机(使环境变量生效):
[test1280@localhost ~]$ echo 'export LD_LIBRARY_PATH=$HOME/libyaml/lib:$LD_LIBRARY_PATH' >> $HOME/.bash_profile
四、安装lyaml库
a.联公网,通过luarocks包管理器下载:
[test1280@localhost ~]$ luarocks install lyaml
报错:
[test1280@localhost ~]$ luarocks install lyaml
Installing https://luarocks.org/lyaml-6.2.5-1.src.rock
Error: Could not find library file for YAML
No file libyaml.a in /usr/local/lib
No file libyaml.a in /usr/local/lib64
No file libyaml.so in /usr/local/lib
No file libyaml.so in /usr/local/lib64
No file matching libyaml.so.* in /usr/local/lib
No file matching libyaml.so.* in /usr/local/lib64
No file libyaml.a in /usr/lib
No file libyaml.a in /usr/lib64
No file libyaml.so in /usr/lib
No file libyaml.so in /usr/lib64
No file matching libyaml.so.* in /usr/lib
No file matching libyaml.so.* in /usr/lib64
No file libyaml.a in /lib
No file libyaml.a in /lib64
No file libyaml.so in /lib
No file libyaml.so in /lib64
No file matching libyaml.so.* in /lib
No file matching libyaml.so.* in /lib64
You may have to install YAML in your system and/or pass YAML_DIR or YAML_LIBDIR to the luarocks command.
Example: luarocks install lyaml YAML_DIR=/usr/local
You may have to install YAML in your system and/or pass YAML_DIR or YAML_LIBDIR to the luarocks command.
Example: luarocks install lyaml YAML_DIR=/usr/local
重新执行YAML_DIR为刚安装的libyaml库:
[test1280@localhost ~]$ luarocks install lyaml YAML_DIR=$HOME/libyaml
安装成功:
[test1280@localhost ~]$ luarocks show lyaml
lyaml 6.2.5-1 - libYAML binding for Lua
Read and write YAML format files with Lua.
License: MIT/X11
Homepage: http://github.com/gvvaughan/lyaml
Installed in: /home/test1280/luarocks
Modules:
lyaml.explicit (/home/test1280/luarocks/share/lua/5.3/lyaml/explicit.lua)
lyaml.functional (/home/test1280/luarocks/share/lua/5.3/lyaml/functional.lua)
lyaml.implicit (/home/test1280/luarocks/share/lua/5.3/lyaml/implicit.lua)
lyaml.init (/home/test1280/luarocks/share/lua/5.3/lyaml/init.lua)
yaml (/home/test1280/luarocks/lib/lua/5.3/yaml.so)
Depends on:
lua >= 5.1, < 5.5 (using 5.3-1)
b.不联公网,预先下载lyaml包,本地安装
下载:
https://github.com/gvvaughan/lyaml/releases
https://github.com/gvvaughan/lyaml/archive/v6.2.5.tar.gz
上传到 $HOME 中,解压缩:
[test1280@localhost ~]$ ll
total 676
drwxrwxr-x. 4 test1280 test1280 32 Jun 10 10:31 libyaml
drwxrwxr-x. 13 test1280 test1280 4096 Jun 10 10:30 libyaml-0.2.5
-rw-r--r--. 1 test1280 test1280 85055 Jun 10 09:11 libyaml-0.2.5.tar.gz
drwxrwxr-x. 7 test1280 test1280 67 Jun 10 10:12 lua
drwxr-xr-x. 4 test1280 test1280 58 Jun 10 10:07 lua-5.3.5
-rw-r--r--. 1 test1280 test1280 303543 Jun 10 08:35 lua-5.3.5.tar.gz
drwxrwxr-x. 6 test1280 test1280 52 Jun 10 10:36 luarocks
drwxr-xr-x. 8 test1280 test1280 4096 Jun 10 10:22 luarocks-3.3.1
-rw-r--r--. 1 test1280 test1280 241636 Jun 9 02:01 luarocks-3.3.1.tar.gz
drwxrwxr-x. 7 test1280 test1280 228 Apr 15 15:31 lyaml-6.2.5
-rw-r--r--. 1 test1280 test1280 48782 Jun 9 05:14 lyaml-6.2.5.tar.gz
通过lyaml包安装:
[test1280@localhost ~]$ cd lyaml-6.2.5/ && luarocks make YAML_DIR=$HOME/libyaml
注意,在这种情况下,当系统中没有libyaml库时,也需要指定变量YAML_DIR到命令行中。
c.更新环境变量,并退出重新登陆主机(使环境变量生效):
[test1280@localhost ~]$ luarocks path
export LUA_PATH='/home/test1280/luarocks/share/lua/5.3/?.lua;/usr/local/share/lua/5.3/?.lua;/usr/local/share/lua/5.3/?/init.lua;/usr/local/lib/lua/5.3/?.lua;/usr/local/lib/lua/5.3/?/init.lua;./?.lua;./?/init.lua;/home/test1280/.luarocks/share/lua/5.3/?.lua;/home/test1280/.luarocks/share/lua/5.3/?/init.lua;/home/test1280/luarocks/share/lua/5.3/?/init.lua'
export LUA_CPATH='/usr/local/lib/lua/5.3/?.so;/usr/local/lib/lua/5.3/loadall.so;./?.so;/home/test1280/.luarocks/lib/lua/5.3/?.so;/home/test1280/luarocks/lib/lua/5.3/?.so'
export PATH='/home/test1280/.luarocks/bin:/home/test1280/lua/bin:/home/test1280/luarocks/bin:/home/test1280/.local/bin:/home/test1280/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin'
将 LUA_PATH、LUA_CPATH、PATH 写入到 $HOME/.bash_profile 中:
[test1280@localhost ~]$ cat $HOME/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PATH=$HOME/luarocks/bin:$PATH
export PATH=$HOME/lua/bin:$PATH
export LD_LIBRARY_PATH=$HOME/libyaml/lib:$LD_LIBRARY_PATH
export LUA_PATH='/home/test1280/luarocks/share/lua/5.3/?.lua;/usr/local/share/lua/5.3/?.lua;/usr/local/share/lua/5.3/?/init.lua;/usr/local/lib/lua/5.3/?.lua;/usr/local/lib/lua/5.3/?/init.lua;./?.lua;./?/init.lua;/home/test1280/.luarocks/share/lua/5.3/?.lua;/home/test1280/.luarocks/share/lua/5.3/?/init.lua;/home/test1280/luarocks/share/lua/5.3/?/init.lua'
export LUA_CPATH='/usr/local/lib/lua/5.3/?.so;/usr/local/lib/lua/5.3/loadall.so;./?.so;/home/test1280/.luarocks/lib/lua/5.3/?.so;/home/test1280/luarocks/lib/lua/5.3/?.so'
export PATH='/home/test1280/.luarocks/bin:/home/test1280/lua/bin:/home/test1280/luarocks/bin:/home/test1280/.local/bin:/home/test1280/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin'
五、验证
代码:
[test1280@localhost ~]$ cat me.lua
lyaml = require "lyaml"
for k, v in pairs(lyaml) do
print(k)
print(v)
end
运行:
[test1280@localhost ~]$ lua me.lua
_VERSION
yaml library for Lua 5.3 / 6.2.5
null
table: 0x15437c0
dump
function: 0x154afa0
load
function: 0x154b440