在-Linux-上安装-Stata

发布时间:2020-10-09 阅读 17

Stata 连享会   主页 || 视频 || 推文

温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。

课程详情 https://gitee.com/arlionn/Course   |   lianxh.cn

课程主页 https://gitee.com/arlionn/Course


目录


Source: Alvaro Carril, Installing Stata on (Ubuntu) Linux

Installing Stata properly on Linux is surprisingly cumbersome, and I’ve found that even Statacorp’s own guide is incomplete and outdated. So here’s mine.

I have installed Stata in many Linux systems, but this tutorial focuses on Ubuntu 16.04 (specially in the last part). I will use Stata 14.2, but with minimal modifications it can be used for versions 13 through 15. I’m also assuming you have a tarball (a .tar.gz file) with the appropriate installation files for your operating system, and that you have root privileges in your system.

1. Installation

  1. Change directory to wherever you have the .tar.gz file (e.g. the Downloads folder). There, create a temporary folder to store the installation files (e.g. statainstall), and extract the installation files to that folder.

    cd ~/Downloads
    mkdir statainstall
    tar -xvzf Stata14Linux64.tar.gz -C statainstall
    
  2. Create the installation directory, and change location into it.

    sudo mkdir /usr/local/stata14
    cd /usr/local/stata14
    
  3. Run the install script.

    sudo ~/Downloads/statainstall/install
    

1.1 License

In order you configure the license file you just need to run ./stinit (you’ll need root privileges to write the file). Be sure to have the serial number, code and authorization. No need to disconnect from the interwebz here, even if you have an “alternative” license ;)

sudo ./stinit

1.2 Add directory to path

In order to be able to launch Stata you’ll need to add the installation directory to the system path.

  1. Add one line at the end of your .bashrc file to add Stata to the unix path.

    echo export PATH="/usr/local/stata14:$PATH" >> ~/.bashrc
    
  2. You need to source your .bashrc so that the changes are effective:

    source ~/.bashrc
    

    (Or you can also restart your terminal)

After adding the install directory to yout path, you should be able to run the appropriate version of Stata from the terminal, e.g.:

xstata-mp

This launches the X-window (GUI based) version of Stata MP. You can also run Stata directly within the terminal with

stata-mp

Obviously you’ll need to substitute mp for whatever version you have a license for. At this point you can also delete the temporary installation folder:

rm -r ~/Downloads/statainstall

2. Additional improvements

I have identified the following potential issues you may have after installing Stata in Linux:

  1. Interface has no icons (ie. only question marks)
  2. Program doesn’t have an application menu entry (ie. can’t search for the app)
  3. Mimetype associations don’t work (ie. you can’t double click a .dta file and have it open in Stata)
  4. PDF documentation links don’t work

If you want to solve the first three of these issues at once with little messing around, you can use Daniel Bela’s stata-integration, which is a bundled Linux binary script integrating an already installed Stata instance into the desktop environment. I’ve tried the script and it works as advertised, solving all above issues. However, it runs some binaries in sudo, so you may be uncomfortable with that.

If you want to address this issues manually, you can check out the sections below.

2.1 Interface icons

Although it is only a aesthetic annoyance, it is annoying to have an interface with no icons:

My good friend and colleague at the NBER, Kyle Barron, has written a fix for this issue. The main advantage of Kyle’s solution is that it doesn’t require sudo privileges.

2.2 Unity launcher and desktop file

Even after successfully installing and running Stata, in Ubuntu it won’t be available as an application in the dash, and it won’t have a proper icon in the application launcher. We can easily fix this by creating a .desktop file for Stata.

sudo gedit /usr/share/applications/stata14.desktop

In this newly-created file just copy and paste the following, obviously adjusting it if you have a different version or flavor of Stata:

[Desktop Entry]
Version=14.2
Terminal=false
Icon=/usr/local/stata14/stata14.png
Type=Application
Categories=Education;Scientific;
Exec=/usr/local/stata14/xstata-mp
Name=Stata/MP 14
Comment=Perform statistical analyses using Stata.
StartupNotify=true
MimeType=application/x-stata-dta;application/x-stata-do;application/x-stata-smcl;application/x-stata-stpr;application/x-stata-gph;application/x-stata-stsem;
Actions=doedit;use;view;graphuse;projmanag;semopen;

After saving this file you should be able to find Stata from the Unity dash, and when launched it should have its icon.

2.3 Adding mimetype associations

Adding mimetype associations for Stata files allows you to see Stata files (e.g. do files, dta files) with their proper icons, and more importantly, to be automagically opened in Stata when executed. This is the default behavior in Windows or Mac, but with Linux we have to do a bit of extra work.

  1. First download this tarball with Stata icons and extract it wherever you like.

  2. In the terminal, go to the location where you have extracted the icons and then change directory to the PNG icons that correspond to your version of Stata. Then issue the following commands:

    xdg-icon-resource install --context mimetypes --size 256 stata-dta_256x256x32.png application-x-stata-dta
    xdg-icon-resource install --context mimetypes --size 256 stata-do_256x256x32.png application-x-stata-do
    
  3. Create and edit the mimetype definitions with the following command:

    sudo gedit /usr/share/mime/packages/application-x-stata.xml
    

    Then copy the following inside this newly created file, and save.

    |  | <?xml version="1.0" encoding="UTF-8"?> |
    |  | <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'> |
    |  | <mime-type type="application/x-stata-dta"> |
    |  | <comment>Stata data file</comment> |
    |  | <glob pattern="*.dta"/> |
    |  | <icon name="application-x-stata-dta"/> |
    |  | </mime-type> |
    |  | <mime-type type="application/x-stata-do"> |
    |  | <comment>Stata do file</comment> |
    |  | <glob pattern="*.do"/> |
    |  | <icon name="application-x-stata-do"/> |
    |  | </mime-type> |
    |  | </mime-info> |
  1. Finally, update the mime and desktop databases so that changes take effect.

    sudo update-mime-database /usr/share/mime
    sudo update-desktop-database /usr/share/applications/
    

That’s it! You should now have a fully functional, “pretty” version of Stata on your Linux system. With a bit of extra work, you can complete the job and add mimetype associations for more obscure Stata files. For instance, I’ve associated do and adofiles to be opened up by Atom (my preferred text editor in Linux), as well as sthlp files (useful for when you’re documenting a program).

2.4 PDF documentation

The PDF manuals are loaded by a script named stata_pdf, located inside your Stata installation directory (eg. /usr/local/stata15/stata_pdf). By default the script points to Acrobat Reader (acroread), but Adobe discontinued it around 2014 (good riddance). We can make some edits to stata_pdf so that it uses Evince, which is the default PDF reader in many Linux distros.

First, it always advisable to backup the original file:

cp /usr/local/stata15/stata_pdf /usr/local/stata15/stata_pdf_bkp

Now we edit the script with nano (or whatever you prefer):

nano /usr/local/stata15/stata_pdf

Scroll past the commented text and modify it so that it reads like the following block. It boils down to three edits: cmd="evince" in second line and the first two wharg=....

case "$PDFVIEWER" in
"")     cmd="evince"
        ;;
*)      cmd="$PDFVIEWER"
        ;;
esac

case "$1" in
"-page")        pagenum=$2
                fname=$3
                wharg="--page-label=$pagenum"
                ;;
"-section")     section=$2
                fname=$3
                wharg="--named-dest=$section"
                ;;
*)              fname="$1"
                wharg=""
                ;;
esac

exec $cmd $wharg "$fname"

3. Known issues

If you have a dark GTK+ theme enabled (e.g. the dark variant of Arc, my theme of choice), the X-window version of Stata looks awful. I tried solving this by executing xstata with a different GTK theme; i.e. something like

 Exec=env GTK_THEME=Arc xstata-mp

but apparently GTK+ 3 applications have to be coded to respect the GTK_THEME env variable, and Stata is not. I suspect some answers in this and this questions might contain possible solutions, but I just switched to a light variant of Arc and called it a day.

相关课程

连享会-直播课 上线了!
http://lianxh.duanshu.com

免费公开课:


课程一览

支持回看,所有课程可以随时购买观看。

专题 嘉宾 直播/回看视频
最新专题 DSGE, 因果推断, 空间计量等
Stata数据清洗 游万海 直播, 2 小时,已上线
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]

Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。


关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。直播间 有很多视频课程,可以随时观看。
  • 连享会-主页知乎专栏,300+ 推文,实证分析不再抓狂。
  • 公众号推文分类: 计量专题 | 分类推文 | 资源工具。推文分成 内生性 | 空间计量 | 时序面板 | 结果输出 | 交乘调节 五类,主流方法介绍一目了然:DID, RDD, IV, GMM, FE, Probit 等。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法

连享会主页  lianxh.cn
连享会主页 lianxh.cn

连享会小程序:扫一扫,看推文,看视频……

扫码加入连享会微信群,提问交流更方便

✏ 连享会学习群-常见问题解答汇总:
https://gitee.com/arlionn/WD